package xiaoa.java.es.utils;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;


import xiaoa.java.es.bean.Params;

/**
 * es搜索工具
 * @author lkc
 * @date 2017年11月14日 下午9:16:38
 * @version V1.0
 *
 */
public class EsSearchUtils {
	
	
	/**
	 * 获取type
	 * @Title: getType
	 * @param params
	 * @return
	 * @author lkc
	 */
	public static  String getType(Params params){
		
		return "content";
	}
	
	
	/**
	 * 获取索引时间字段名字
	 * @Title: getIndexField
	 * @param params
	 * @return
	 * @author lkc
	 */
	public static String getIndexField(Params params){
		
		return "IndexTime";
		
	}
	
	
	/**
	 * 创建查询query
	 * @Title: createLuceneQuery
	 * @param params
	 * @return
	 * @author xiaoa
	 */
	public static Query createLuceneQuery(Params params){
		
		if (params == null){
			return new BooleanQuery.Builder().build();
		}
		
		BooleanQuery.Builder  query = new BooleanQuery.Builder();
		
		if (params.getUserName() != null && !params.getUserName().isEmpty()){
			query.add(new TermQuery(new Term("userName", params.getUserName())), Occur.MUST);
		}
		
		if(query.build().toString().length() == 0){
			query.add(new TermQuery(new Term("*", "*")), Occur.SHOULD);
		}
		
		return query.build();
		
	}
	
	
}
